//
//  AEPersonTopView.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/12.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEPersonTopView.h"
#import "AEPersonViewModel.h"
#import "AE_Solar-Swift.h"

@interface AEPersonTopView ()
@property (nonatomic, strong) AEPersonViewModel *viewModel;
@property (nonatomic, strong) UIImageView *bgImgView;

@property (nonatomic, strong) UIView *statusNavView;

@property (nonatomic, strong) UIView *navView;

/// 标题
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIImageView *topBottomBgView;//ic_personal_top_bottom_bg

/// 我的积分
@property (nonatomic, strong) UILabel *myBonusLabel;

/// 积分数值
@property (nonatomic, strong) UILabel *cntLabel;

/// 等级
@property (nonatomic, strong) UILabel *levelLabel;

@property (nonatomic, strong) UIView *sharePointsBgView;

/// 分享领积分
@property (nonatomic, strong) UILabel *sharePointsLabel;

/// 反馈
@property (nonatomic, strong) TYZImageTitleButton *btnFeedback;

/// 头像
@property (nonatomic, strong) UIImageView *headerImgView;

/// 姓名
@property (nonatomic, strong) UILabel *nameLabel;

/// 账号id
@property (nonatomic, strong) UILabel *accountLabel;

/// 设置
@property (nonatomic, strong) UIButton *btnSetting;

@end

@implementation AEPersonTopView

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithBgImgView];
    
    [self initWithStatusNavView];
    
    [self initWithNavView];
    
    [self initWithTitleLabel];
    
    [self initWithBtnSetting];
    
    [self initWithTopBottomBgView];
}

- (void)initWithBgImgView {
    CGRect frame = self.bounds;
    UIImage *image = [UIImage imageNamed:@"ic_personal_top_bg"];
    _bgImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
        
    }];
}

- (void)initWithStatusNavView {
    CGRect frame = CGRectMake(0, 0, self.width, NAVBAR_HEIGHT_STATUS);
    _statusNavView = [[UIView alloc] initWithFrame:frame];
//    _statusNavView.backgroundColor = [UIColor systemPinkColor];
    [self addSubview:_statusNavView];
}

- (void)initWithNavView {
    CGRect frame = CGRectMake(0, self.statusNavView.height - NAVBAR_HEIGHT_NOTOPSPACE, self.width, NAVBAR_HEIGHT_NOTOPSPACE);
    _navView = [[UIView alloc] initWithFrame:frame];
//    _navView.backgroundColor = [UIColor orangeColor];
    [self.statusNavView addSubview:_navView];
}

- (void)initWithTitleLabel {
    if (!_titleLabel) {
        CGRect frame = CGRectMake(80, 0, self.width - 80 * 2, self.navView.height);
        _titleLabel = [UILabel createWithLabel:self.navView frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
}

/// 设置
- (void)initWithBtnSetting {
    if (!_btnSetting) {
        UIImage *image = [UIImage imageNamed:@"ic_mine_setting"];
        CGRect frame = CGRectMake(self.navView.width - 30 - 12, (self.navView.height - 30) * 0.5, 30, 30);
        _btnSetting = [UIButton createWithButton:self.navView frame:frame font:FONTSIZE(16) target:self action:@selector(actionWithSetting:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setImage:image forState:UIControlStateNormal];
        }];
    }
}

/// 下面背景图片
- (void)initWithTopBottomBgView {
    if (!_topBottomBgView) {
        UIImage *image = [UIImage imageNamed:@"ic_personal_top_bottom_bg"];
        CGRect frame = CGRectMake(20, self.height - image.size.height, self.width - 20 * 2, image.size.height);
        _topBottomBgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.userInteractionEnabled = YES;
//            imageView.backgroundColor = [UIColor redColor];
        }];
    }
}

/// 我的积分
- (void)initWithMyBonusLabel {
    if (!_myBonusLabel) {
        CGRect frame = CGRectMake(100, 16, self.topBottomBgView.width - 100 * 2, 18);
        UIFont *font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
        _myBonusLabel = [UILabel createWithLabel:self.topBottomBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    
    self.myBonusLabel.text = self.viewModel.myBonus;
}

/// 积分数值
- (void)initWithCntLabel {
    if (!_cntLabel) {
        CGRect frame = CGRectMake(self.myBonusLabel.left, self.myBonusLabel.bottom + 3, self.myBonusLabel.width, 42);
        UIFont *font = [UIFont systemFontOfSize:30 weight:UIFontWeightBold];
        _cntLabel = [UILabel createWithLabel:self.topBottomBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    if (self.viewModel && self.viewModel.userInfoModel) {
        self.cntLabel.text = objectNull(self.viewModel.userInfoModel.scoreNum);
    }
}

/// 等级
- (void)initWithLevelLabel {
    if (!_levelLabel) {
        CGRect frame = CGRectMake(20, self.cntLabel.bottom, self.topBottomBgView.width - 20 * 2, 18);
        UIFont *font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
        _levelLabel = [UILabel createWithLabel:self.topBottomBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
        }];
    }
    self.levelLabel.text = [NSString stringWithFormat:self.viewModel.currentLevel, 1];
}

- (void)initWithSharePointsBgView {
    if (!_sharePointsBgView) {
        CGRect frame = CGRectMake(self.topBottomBgView.width - 76, 50, 76, 24);
        _sharePointsBgView = [[UIView alloc] initWithFrame:frame];
        _sharePointsBgView.backgroundColor = [UIColor colorWithHexString:@"#f1a9ab"];
        CAShapeLayer *shapeLayer = [CALayer bezierPathRound:_sharePointsBgView.bounds cornerRadii:CGSizeMake(24, 24) roundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft];
        _sharePointsBgView.layer.mask = shapeLayer;
        [self.topBottomBgView addSubview:_sharePointsBgView];
        
        @weakify(self);
        // 点击分享领积分
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            @strongify(self);
            if (self.touchSharePointsBlock) {
                self.touchSharePointsBlock();
            }
        }];
        [self.sharePointsBgView addGestureRecognizer:tap];
    }
    UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    float fontWidth = [self.viewModel.shareBonus widthForFont:font] + 2;
    float width = 14 + 2 + fontWidth;
    self.sharePointsBgView.width = width;
    self.sharePointsBgView.left = self.topBottomBgView.width - width + 3;
}

/// 分享领积分
- (void)initWithSharePointsLabel {
    if (!_sharePointsLabel) {
        CGRect frame = CGRectMake(14, 0, 0, 24);
        UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
        _sharePointsLabel = [UILabel createWithLabel:self.sharePointsBgView frame:frame font:font propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#C00000"];
        }];
    }
    float fontWidth = [self.viewModel.shareBonus widthForFont:self.sharePointsLabel.font] + 2;
    float width = 0 + fontWidth;
    self.sharePointsLabel.left = 12;
    self.sharePointsLabel.width = width;
    self.sharePointsLabel.text = [NSString stringWithFormat:@"%@", self.viewModel.shareBonus];
}

/// 初始化反馈
- (void)initWithBtnFeedback {
    UIImage *image = [UIImage imageNamed:@"ic_mine_feedback"];
    UIFont *font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
    if (!_btnFeedback) {
        float width = image.size.width + 5 + [self.viewModel.feedback widthForFont:font] + 2;
        CGRect frame = CGRectMake(self.width - width - 20, self.topBottomBgView.top - 40 - 22, width, 22);
        _btnFeedback = [TYZImageTitleButton createWithImageButton:self frame:frame font:font target:self action:@selector(actionWithFeedback:) propertiesBlock:^(TYZImageTitleButton * _Nonnull button) {
            [button setTitle:self.viewModel.feedback forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
            [button setImage:image forState:UIControlStateNormal];
            [button layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:4];
        }];
    }
    
    float width = image.size.width + 5 + [self.viewModel.feedback widthForFont:font] + 2;
    _btnFeedback.width = width;
    _btnFeedback.left = self.width - width - 20;
    [_btnFeedback setTitle:self.viewModel.feedback forState:UIControlStateNormal];
    [_btnFeedback layoutButtonWithEdgeInsetsStyle:TYZButtonEdgeInsetsStyleLeft imageTitleSpace:4];
}

/// 头像
- (void)initWithHeaderImgView {
    if (!_headerImgView) {
        UIImage *image = [UIImage imageNamed:@"ic_header_img"];
        CGRect frame = CGRectMake(20, self.topBottomBgView.top - 17 - 60, 60, 60);
        _headerImgView = [UIImageView createWithImageView:self frame:frame defaultImage:image propertiesBlock:^(UIImageView * _Nonnull imageView) {
            imageView.layer.cornerRadius = frame.size.height * 0.5;
            imageView.layer.masksToBounds = YES;
//            imageView.backgroundColor = [UIColor orangeColor];
        }];
    }
}

/// 姓名
- (void)initWithNameLabel {
    if (!_nameLabel) {
        CGRect frame = CGRectMake(self.headerImgView.right + 14, self.headerImgView.top, self.btnFeedback.left - self.headerImgView.right - 14 - 8, 24);
        _nameLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:18 weight:UIFontWeightBold] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#3E3A39"];
//            label.text = @"于先生";
        }];
    }
    if (self.viewModel && self.viewModel.userInfoModel) {
        self.nameLabel.text = objectNull(self.viewModel.userInfoModel.name);
    }
}

/// 账号
- (void)initWithAccountLabel {
    if (!_accountLabel) {
        CGRect frame = self.nameLabel.frame;
        frame.size.height = 18;
        frame.origin.y = self.headerImgView.bottom - 5 - frame.size.height;
        _accountLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:13 weight:UIFontWeightMedium] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#C00000"];
//            label.text = @"169829816";
        }];
    }
    if (self.viewModel && self.viewModel.userInfoModel) {
        self.accountLabel.text = [NSString stringWithFormat:@"%@", self.viewModel.userInfoModel.id];
    }
}

/// 反馈
/// - Parameter sender: sender description
- (void)actionWithFeedback:(id)sender {
    if (self.touchFeedbackBlock) {
        self.touchFeedbackBlock();
    }
}

/// 设置
/// - Parameter sender: sender description
- (void)actionWithSetting:(id)sender {
    if (self.touchSettingBlock) {
        self.touchSettingBlock();
    }
}

- (void)updateWithViewData:(id)data {
    self.viewModel = data;
    self.titleLabel.text = self.viewModel.title;
    
    [self initWithMyBonusLabel]; // 我的积分标题
    
    [self initWithCntLabel]; // 积分
    
    [self initWithLevelLabel]; // 等级
    
    [self initWithSharePointsBgView]; // 分享积分背景
    
    [self initWithSharePointsLabel]; // 分享积分
    
    [self initWithBtnFeedback]; // 反馈
    
    [self initWithHeaderImgView]; // 头像
    
    [self initWithNameLabel]; // 姓名
    
    [self initWithAccountLabel]; // 账号
}

@end
